<?php

class AjaxController extends Zend_Controller_Action {

    protected $_log;
    protected $doctrineContainer;

    public function init() {
        $this->_helper->layout()->disableLayout();
        $this->_log = Zend_Registry::get('log');
        $this->doctrineContainer = Zend_Registry::get('doctrine');
    }
    
    public function indexAction() {

    }
    
    public function gatewayusbhubAction() {
        $method = $this->_getParam('method');
        $hubId  = $this->_getParam('hubId');
        $uri    = $this->_request->getPost('uri');
        $gateway = new Application_Model_SystemGateway();
        $convertedJSON = json_decode($gateway->$method($hubId),true);  
        foreach ($convertedJSON as $key => $value) {
            $convertedJSON[$key]['date'] = $this->view->datetime($convertedJSON[$key]['date']);     
        }
        $this->view->response = json_encode($convertedJSON);
    }
 #/ajax/gatewayusbhub/method/getNewUSBHubs
 #/ajax/gatewayusbhub/method/markHubAwaiting/hubId/' + hubId
    public function systempingAction() {
        $hubMethod  = $this->_getParam('hubMethod');
        $hubId      = $this->_getParam('hubId');
        $uri        = $this->_request->getPost('uri');        
        
        #$key    = $this->_getParam('key');

        $object = array();
        $gateway = new Application_Model_SystemGateway();
        
        #NewUSBHubs / USB hubs operations
        if ($hubMethod) {
            $convertedJSON = json_decode($gateway->$hubMethod($hubId),true);  
            foreach ($convertedJSON as $key => $value) {
                $convertedJSON[$key]['date'] = $this->view->datetime($convertedJSON[$key]['date']);     
            }
            $object['NewUSBHubs'] = $convertedJSON;
        }
        
        #SystemStatus / USB hubs operations
        $object['SystemStatus'] = array(
                                      "system" => "OK",
                                      "devices" => 2
                                     );
        
        if ($uri) {
            switch($uri) {
                case '/agents';
                case '/index/dashboard';
                case '/hubs';             
                case '/devices';
                    $object[$uri] = array(
                                      "hfhfhf" => "OK",
                                      "gfhfhf" => "erfdfs"
                                     );
                    break;
                default;
                    break;
            }
        }
        $this->view->response = json_encode($object);
    }
    
    public function setorderAction() {
        $this->getHelper('viewRenderer')->setNoRender();
        $em = $this->doctrineContainer->getEntityManager();        
      
        $order = $this->_getParam('order');
        $page  = $this->_getParam('page');
        
        $sortStart = 1000;
        $sortStep = 10;
        
        $ids = explode(',',$order);
        
        if ($page === "devices") {
            foreach($ids as $id) {
                if($id > 0) {
                    $device = $em->getRepository('Jhub1db\Entity\Jhub1device')->findBy(array('id' => $id));
                    $device[0]->setOrder($sortStart);
                    $sortStart = $sortStart + $sortStep;
                    $em->persist($device[0]);    
                    $em->flush();
                }
            }
            
        } else if ($page === "hubs") {
            foreach($ids as $id) {
                 if($id > 0) {
                    $hub = $em->getRepository('Jhub1db\Entity\Jhub1service')->findBy(array('id' => $id));
                    $hub[0]->setOrderi($sortStart);
                    $sortStart = $sortStart + $sortStep;
                    $em->persist($hub[0]);    
                    $em->flush();
                 }
            }
            
        } else if ($page === "agents") {
            foreach($ids as $id) {
                 if($id > 0) {
                    $agent = $em->getRepository('Jhub1db\Entity\Jhub1agent')->findBy(array('id' => $id));
                    $agent[0]->setOrderi($sortStart);
                    $sortStart = $sortStart + $sortStep;
                    $em->persist($agent[0]);    
                    $em->flush();
                 }
            }            
        }

    }
}
